package ch15
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    import utils.Ball3D;
    
    /**
     * 3D缓动
     * @author jianping.shenjp
     */
    public class Easing3D extends Sprite
    {
        private var ball:Ball3D;
		//目标点
        private var tx:Number;
        private var ty:Number;
        private var tz:Number;
		//缓动系数
        private var easing:Number = .1;
		//fl [焦距：focal length]，相当于人眼的电脑屏幕的距离
        private var fl:Number = 250;
		//消失点
        private var vpX:Number = stage.stageWidth / 2;
        private var vpY:Number = stage.stageHeight / 2;
        
        public function Easing3D()
        {
            init();
        }
        
        private function init():void
        {
            ball = new Ball3D();
            addChild(ball);
            tx = Math.random() * 500 - 250;
            ty = Math.random() * 500 - 250;
            tz = Math.random() * 500;
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(event:Event):void
        {
            var dx:Number = tx - ball.xpos;
            var dy:Number = ty - ball.ypos;
            var dz:Number = tz - ball.zpos;
            ball.xpos += dx * easing;
            ball.ypos += dy * easing;
            ball.zpos += dz * easing;
			//3D 距离
            var dist:Number = Math.sqrt(dx * dx + dy * dy + dz * dz);
            if (dist < 1)
            {
                tx = Math.random() * 500 - 250;
                ty = Math.random() * 500 - 250;
                tz = Math.random() * 500;
            }
            if (ball.zpos > -fl)
            {
                var scale:Number = fl / (fl + ball.zpos);
                ball.scaleX = ball.scaleY = scale;
                ball.x = vpX + ball.xpos * scale;
                ball.y = vpY + ball.ypos * scale;
                ball.visible = true;
            }
            else
            {
                ball.visible = false;
            }
        }
    }
}